/**  
* <p>Title: RedisSessionTest.java</p>  
* <p>Description: </p>  
* <p>Copyright: Copyright (c) 2017</p>  
* @author Administrator
* @date 2020年2月27日  
* @version 1.0  
*/  
package com.skivingcloud.admin.utils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.SimpleSession;

import com.skivingcloud.admin.sys.entity.User;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**  
* <p>Title: RedisSessionTest</p>  
* <p>Description: </p>  
* @author Administrator  
* @date 2020年2月27日  
*/
public class RedisSessionTest {

	public static void main(String[] args) {
		//创建连接池对象  host:连接redis主机IP ；port:redis的默认端口
		 JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), "127.0.0.1", 6379, 3000000, "1qaz!QAZ3edc", 0);
		//获取连接资源
		 Jedis resource = jedisPool.getResource();
		 byte[] hello = resource.get("shiro:session:997c85ca-606b-4c32-affa-acda2d79978a".getBytes());//.get("shiro:session:6385811a-7498-45cf-abb9-cda2f9339864");
	 	Session session = byteToSession(hello); 
	 	User user = (User)session.getAttribute("userSession");
	 	System.out.println("----------"+user.getStaffName());
	 	resource.close();
	 }
	 
	// 把byte还原为session
	    public static Session byteToSession(byte[] bytes){
	        ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
	        ObjectInputStream in;
	        SimpleSession session = null;
	        try {
	            in = new ObjectInputStream(bi);
	            session = (SimpleSession) in.readObject();
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    
	        return session;
	    }

}
